package com.ccb.lms.action;


import java.util.List;

import com.ccb.lms.facade.FacadeService;
import com.ccb.lms.model.*;
import com.ccb.lms.util.BasePage;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class Login extends ActionSupport implements ModelDriven<User>, Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5616324113451465026L;
	
	private FacadeService facadeService;
	private String id;
	private int pageIndex=1;
	private String pageBar;
	private List<User> list;
	private User user;
	
	private Reader reader;
	private LibraryAdmin libraryAdmin;
	private SystemAdmin systemAdmin;

	private int currentPage;
	private int showRecordNum;
	private int totalPages;
	private BasePage bp;
	

	
	public void prepare()throws Exception{
		if(id==null||id.length()==0){
			user=new User();
		}else{
			user=facadeService.getUserService().getUserById(Integer.parseInt(id));
		}
	}
	
	
	public String execute()throws Exception{
		if(facadeService.getUserService().isLogin(user)){
			
			return SUCCESS;
		}
		
		if(facadeService.getReaderService().isLogin(reader)){
			return SUCCESS;
		}
		
		return INPUT;
	}
	public String save()throws Exception{
		if(facadeService.getUserService().insertUser(user)){
			return SUCCESS;
		}
		return ERROR;
	}
	public String edit(){
		return SUCCESS;
	}
	public String update()throws Exception{
		if(facadeService.getUserService().updateUser(user)){
			return SUCCESS;
		}
		return ERROR;
	}
	public String delete()throws Exception{
		if(facadeService.getUserService().deleteUser(Integer.parseInt(id))){
			return SUCCESS;
		}
		return ERROR; 
 	}
	public String findAllUser()throws Exception{
		try {
			list=facadeService.getUserService().getAllUser();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String findUserByPage() throws Exception {
		try {
			if (showRecordNum == 0) {
				showRecordNum = 10;
			}

			bp = facadeService.getUserService().getBasePage(currentPage,showRecordNum);//
			list = facadeService.getUserService().getUserByPage(bp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public String getPageBar() {
		return pageBar;
	}

	public void setPageBar(String pageBar) {
		this.pageBar = pageBar;
	}

	public List<User> getList() {
		return list;
	}

	public void setList(List<User> list) {
		this.list = list;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public int getShowRecordNum() {
		return showRecordNum;
	}


	public void setShowRecordNum(int showRecordNum) {
		this.showRecordNum = showRecordNum;
	}


	public int getTotalPages() {
		return totalPages;
	}


	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}


	public BasePage getBp() {
		return bp;
	}


	public void setBp(BasePage bp) {
		this.bp = bp;
	}


	public FacadeService getFacadeService() {
		return facadeService;
	}
	public void setFacadeService(FacadeService facadeService) {
		this.facadeService = facadeService;
	}
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}
	
}
